$(function () {
  layui.use(['element', 'layer', 'util'], function(){
    var element = layui.element
    ,layer = layui.layer
    ,util = layui.util
    ,$ = layui.$;
    
    //头部事件
    util.event('lay-header-event', {
      //左侧菜单事件
      menuLeft: function(othis){
        layer.msg('展开左侧菜单的操作', {icon: 0});
      }
      ,menuRight: function(){
        layer.open({
          type: 1
          ,content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
          ,area: ['260px', '100%']
          ,offset: 'rt' //右上角
          ,anim: 5
          ,shadeClose: true
        });
      }
    });
    
  })})
  //个人信息获取
  getUserInfo()
  function getUserInfo() {
    axios({
      url:'/my/userinfo',
      method:'get',
    }).then(({data:res})=>{
      //成功回调
      if (res.status !== 0) {
        return layer.msg(res.message)
      }
      console.log(res)
      renderAvatar(res.data)
    });
  }
  
  //个人信息渲染
  function renderAvatar(obj) {
    let name = obj.nickname || obj.username
    $('.welcome').text(` 欢迎 ${name}`)
    if (obj.user_pic) {
      $('.layui-nav-img').prop('src', obj.user_pic).show()
      $('.text-avatar').hide()
    } else {
      $('.text-avatar').text(name[0]).siblings('.layui-nav-img').hide()
    }
  }
  //退出
  $('.logout').on('click',function(){
    layer.confirm('确认退出登录吗？', {icon: 3, title:'提示'}, function(index){
      //do something
      localStorage.removeItem('token')
      location.href='/login.html'
      layer.close(index);
    });
  })
